document.getElementById("key").onkeyup=function(){
	if(event.keyCode==13){
		search()	
	}
}




function showSuccessNotic(info) {
    var element = "";
    if (window.innerWidth > 737) {
        var t = document.getElementById("showSuccessNotic");
        if (info !== undefined) {
            //后端额外附加信息
            t.textContent = "操作成功," + info + "!";
        }
        else {
            t.textContent = "操作成功!";
        }
        $("#showSuccessNotic").slideDown("fast");
    }
    else {
        var t = document.getElementById("showSuccessNoticeIphone");
        t.style.display = "block";
    }
    element = t;
    setTimeout(function () {
        //element.style.display = "none"
        $("#showSuccessNotic").slideUp("slow");
    }, 2000)//三秒钟过后，自动消失

}


function search(){
	var key=document.getElementById("key").value;
	if(/^ *$/.test(key)){
		alert("请输入关键字！")
		return false;
	}
	jQuery.ajax({
		"url":"/search_teacher/",
		"data":{"key":key},
		"dataType":"jsonp",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"success":function(data){
			if (!data.status){
				alert(data.content)
				return false;
			}
			else{
				createTab(data.content);
			}
		
		}
		
	})
}
$("#search").on("click",function(){
	search()
})

function createTab(data){
	$("tbody>tr").remove()
	var tab=document.getElementById("tab");
	for(var i=0;i<data.length;i++){
		var tr=document.createElement("tr");
		tr.style.cursor="pointer";
	
	
		var td=document.createElement("td");
		td.textContent=data[i].id;
		tr.appendChild(td);

		
		var td=document.createElement("td");
		td.textContent=data[i].name;
		tr.appendChild(td);


		var td=document.createElement("td");
		td.textContent=data[i].teacher_name;
		tr.appendChild(td);
		
		var td=document.createElement("td");
		
    		var addButton = document.createElement("div");
		status=data[i].status;
		if (status==="未加入"){
    			addButton.className = "btn btn-success btn-xs ";
			addButton.textContent="加入"
    			addButton.setAttribute("sid", data[i].id);//服务器的每一个参数在className中
    			addButton.setAttribute("data", JSON.stringify(data[i]));//服务器的每一个参数在className中
			
    			addButton.onclick = function () {
				var data=this.getAttribute("data")
				data=JSON.parse(data)
				t=confirm("请确认如下信息：\n1. 班级编号：" +data.id + "\n2. 班级名称：" +data.name+"\n3. 老师姓名：" + data.teacher_name+"\n真的要加入吗？")
				if(t===false){
					return false;
				}
				var sid=this.getAttribute("sid");
				joinClass(sid,this);
		        }
		}
		else{
    			addButton.className = "btn btn-primary btn-xs ";
			addButton.textContent="已加入"
			addButton.setAttribute("disabled",true)
		}
		td.appendChild(addButton)
		tr.appendChild(td);
		tab.appendChild(tr);

	}
}

function joinClass(sid,button){
	jQuery.ajax({
		"url":"/join_class/",
		"data":{"sid":sid},
		"dataType":"jsonp",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content);
				return false;
			}
			else{
				showSuccessNotic();
				button.textContent="已加入"
				button.style.background="#8b8d98de"
				button.disabled;
				button.onclick=null;
				alert("已发送申请，请等待老师通过！")
				window.opener.location.reload()
				window.close()
				
			}
		}
	})
}

